﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
 
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Core.Extension;
using RBS.Web.API.Models;
 
namespace RBS.Web.API.Areas.Hotel.Controllers
{
   /// <summary>
   /// 菜谱分类
   /// </summary>
    public class CookbookController : ApiController
    {
        private ApiContext db = new ApiContext();
        /// <summary>
        /// 获取酒店菜谱分类
        /// </summary> 
        /// <param name="hid">酒店ID</param>
        /// <returns></returns> 
        public JsonData GetCookbookCategory(int hid)
        { 
            var jsonData = new JsonData { Success = true };
          /*  if (type == null)
            {
                jsonData.Error = "请输入要调用的类型";
                jsonData.Success = false;
                return jsonData;
            }*/
            if (hid <= 0)
            {
                jsonData.Error = "请输入酒店ID";
                jsonData.Success = false;
                return jsonData;
            }
            try
            {
                var items = db.HotelCookbookCategory.Where(c => c.HotelId == hid && c.IsDisplay == 0).OrderBy(s => s.Code).ToList();
                jsonData.Data = items;
            }
            catch (Exception ex)
            {
                jsonData.Error = ex.Message;
                jsonData.Success = false;
            }
            return jsonData;
        } 

        /// <summary>
        /// 获取酒店菜谱
        /// </summary>
        /// <param name="type">菜谱类型</param>
        /// <param name="hid">酒店ID</param>
        /// <param name="cid">菜谱分类ID</param>
        /// <returns></returns> 
        public JsonData GetCookbook(string type, int hid,int cid=0)
        {
           
           var jsonData = new JsonData { Success = true };
            if (type == null)
            {
                jsonData.Error = "请输入要调用的类型";
                jsonData.Success = false;
                return jsonData;
            }
            if (hid <= 0)
            {
                jsonData.Error = "请输入酒店ID";
                jsonData.Success = false;
                return jsonData;
            }
            try
            {
    
                if (cid == 0)
                {
                    var query = from p in db.HotelCookbook
                                join pp in db.HotelCookbookCategory
                                on p.CategoryId equals pp.Id
                                where pp.HotelId == hid && pp.IsDisplay==0 && p.EstimateClear!="0" 
                                orderby pp.Code
                                select new
                                {
                                    p.Id,
                                    Name = p.Name + "（" + p.Unit + "）", 
                                    Price =p.Discount ,
                                    p.Unit,
                                    p.DishImages
                                };
                    jsonData.Data = query.ToList(); 
                }
                else
                {
                    var items = db.HotelCookbook.Where(c => c.CategoryId == cid && c.EstimateClear != "0").Select(p => new
                                {
                                    p.Id,
                                    Name = p.Name + "（" + p.Unit + "）",
                                    Price = p.Discount,
                                    p.Unit,
                                    p.DishImages
                                });
                    jsonData.Data = items; 
                } 
            }
            catch (Exception ex)
            {
                jsonData.Error = ex.Message;
                jsonData.Success = false;
            }
            return jsonData;
            
          
        }
    }
}
